<template>
  <!-- 放置一个图标 -->
  <div class="screenWrapper">
    <svg-icon
      :icon-class="isFull ? 'exit-fullscreen' : 'fullscreen'"
      class="fullscreen"
      @click="toggleScreen"
    />
  </div>
</template>

<script>
export default {
  name: 'ScreenFull',
  data() {
    return {
      // 是否全屏的状态，false 为非全屏
      isFull: false
    }
  },
  mounted() {
  // 监听全屏状态变化
    document.addEventListener('fullscreenchange', () => {
    // document.fullscreenElement 是当前浏览器的全屏状态，但是它返回不是布尔值，所以需要 !! 转换为布尔值
    // 使 isFull 状态，跟当前浏览器的全屏状态保持一致
      this.isFull = !!document.fullscreenElement
    })
  },
  methods: {
    toggleScreen() {
      // 当前不是全屏，点击后，开启全屏状态
      // 当前是全屏，点击后，退出全屏状态
      if (!this.isFull) {
        document.documentElement.requestFullscreen()
      } else {
        document.exitFullscreen()
      }
      this.isFull = !this.isFull
    }
  }
}
</script>

<style lang="scss" scoped>
.screenWrapper {
  display: flex;
  align-items: center;
  margin-right: 20px;
}
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
